﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using Microsoft.Reporting.WinForms;

namespace Billing.WindowsBase
{
    public partial class ReportFormBase : Billing.WindowsBase.FormBase
    {
        public event EventHandler ReportLoaded;
        public event EventHandler<CancelEventArgs> Print;
        public event EventHandler<CancelEventArgs> ReportRefresh;
        public event EventHandler<CancelEventArgs> RenderingBegin;
        public event EventHandler<Microsoft.Reporting.WinForms.RenderingCompleteEventArgs> RenderingComplete;

        private string reportName;
        private IList<ReportParameter> parameters;
        private ProcessingMode processingMode= ProcessingMode.Remote;
        private bool mCloseAfterPrint;
        private bool mPrintPreview = true;
        private bool mInnerPrintengine = false;
        private object dataSource;
        private System.Reflection.Assembly assembly;

        public System.Reflection.Assembly Assembly
        {
            get { return assembly; }
            set { assembly = value; }
        }
        public bool PrintPreview
        {
            get { return mPrintPreview; }
            set { mPrintPreview = value; }
        }
        public bool InnerPrintEngine
        {
            get { return mInnerPrintengine; }
            set { mInnerPrintengine = value; }
        }
        public bool CloseAfterPrint
        {
            get { return mCloseAfterPrint; }
            set { mCloseAfterPrint = value; }
        }
        public ProcessingMode ProcessingMode
        {
            get { return processingMode; }
            set { processingMode = value; }
        }
        public string ReportName
        {
            get { return reportName; }
            set
            {
                this.reportName = value;
            }
        }
        public IList<ReportParameter> Parameters
        {
            get { return parameters; }
        }
        public object DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }

        public void AddParameter(string name, string value)
        {
            AddParameter(name, value, true);
        }

        public void AddParameter(string name, string value, bool visible)
        {
            this.parameters.Add(new ReportParameter(name, value, visible));
        }

        public ReportFormBase()
            : base()
        {

        }

        public ReportFormBase(string reportName)
            : this(reportName, ProcessingMode.Remote)
        { }

        public ReportFormBase(string reportName, ProcessingMode processingMode)
            : this(reportName, processingMode, new List<ReportParameter>())
        {

        }

        public ReportFormBase(string reportName, ProcessingMode processingMode, IList<ReportParameter> parameters)
            : this()
        {
            this.reportName = reportName;
            this.processingMode = processingMode;
            this.parameters = parameters;
        }

        public void LoadReport()
        {
            LoadReport(reportName);
        }

        public void LoadReport(string reportName)
        {
            LoadReport(reportName, processingMode);
        }

        public void LoadReport(string reportName, ProcessingMode processingMode)
        {
            LoadReport(reportName, processingMode, parameters);
        }

        public void LoadReport(string reportName, ProcessingMode processingMode, IList<ReportParameter> parameters)
        {
            this.reportName = reportName;
            this.parameters = parameters;
            this.processingMode = processingMode;
            OnLoadReport(EventArgs.Empty);
        }

        public virtual void RefreshReport()
        {
            base.Refresh();
        }

        protected virtual void OnPrint(CancelEventArgs e)
        {
            if (Print != null)
                Print(this, e);
        }

        protected virtual void OnReprotRefresh(CancelEventArgs e)
        {
            if (ReportRefresh != null)
                ReportRefresh(this, e);
        }

        protected virtual void OnLoadReport(EventArgs e)
        {
            if (ReportLoaded != null)
                ReportLoaded(this, e);
        }

        protected virtual void OnRenderingBegin(CancelEventArgs e)
        {
            if (RenderingBegin != null)
                RenderingBegin(this, e);
        }

        protected virtual void OnRenderingComplete(Microsoft.Reporting.WinForms.RenderingCompleteEventArgs e)
        {
            if (RenderingComplete != null)
                RenderingComplete(this, e);
        }

    }
}
